<?php
class Tv_Arte_Utils_Str
{
    /**
     * getFileExtension
     *
     * @static
     * @param   String $s
     * @return  mixed   String of false
     */
    public static function getFileExtension($s)
    {
        preg_match('/\.([a-z]+)$/i', $s, $matches);
        return (count($matches) == 2) ? $matches[1] : false;
    }
    /**
     * removeAccents
     *
     * @static
     * @param   string $s
     * @return  String
     */
    public static function removeAccents($s)
    {
        $s = preg_replace('/[àáâãäå]/u', 'a', $s);
        $s = preg_replace('/[ÀÁÂÃÄÅ]/u', 'A', $s);
        $s = preg_replace('/[èéêë]/u', 'e', $s);
        $s = preg_replace('/[ËÉÊÈ]/u', 'E', $s);
        $s = preg_replace('/[ìíîï]/u', 'i', $s);
        $s = preg_replace('/[ÌÍÎÏ]/u', 'I', $s);
        $s = preg_replace('/[ðòóôõöø]/u', 'o', $s);
        $s = preg_replace('/[ÐÒÓÔÕÖØ]/u', 'O', $s);
        $s = preg_replace('/[ùúûü]/u', 'u', $s);
        $s = preg_replace('/[ÙÚÛÜ]/u', 'U', $s);
        $s = preg_replace('/[ýýÿ]/u', 'y', $s);
        $s = preg_replace('/[ÝÝŸ]/u', 'Y', $s);
        $s = preg_replace('/[ç]/u', 'c', $s);
        $s = preg_replace('/[Ç]/u', 'C', $s);
        $s = preg_replace('/[ñ]/u', 'n', $s);
        $s = preg_replace('/[Ñ]/u', 'N', $s);
        $s = preg_replace('/[š]/u', 's', $s);
        $s = preg_replace('/[Š]/u', 'S', $s);
        $s = preg_replace('/[ž]/u', 'z', $s);
        $s = preg_replace('/[Ž]/u', 'Z', $s);
        $s = preg_replace('/[æ]/u', 'ae', $s);
        $s = preg_replace('/[Æ]/u', 'AE', $s);
        $s = preg_replace('/[œ]/u', 'oe', $s);
        $s = preg_replace('/[Œ]/u', 'OE', $s);
        return $s;
    }
    /**
     * sanitizeFileName
     *
     * @static
     * @param   String $s
     * @return  String
     */
    public static function sanitizeFileName($s)
    {
        $s = self::removeAccents($s);
        $s = preg_replace('/[ ]/', '-', $s);
        $s = preg_replace('/[^a-zA-Z0-9\-\._]/', '-', $s);
        $s = preg_replace('/(\-\-)/', '', $s);
        return $s;
    }
    /**
     * arrayToAttributes
     *
     * @static
     * @param   Array $array
     * @return  String
     */
    public static function arrayToAttributes($array)
    {
        $s = "";
        foreach ($array as $key => $value) {
            $s .= " " . $key . "=\"" . $value . "\"";
        }
        return trim($s);
    }
    /**
     * parseCssByString
     *
     * @param   String
     * @return  Array
     */
    public static function parseCssByString($css)
    {
        $cssArray = Array();
        preg_match_all('/([#.][a-z0-9_-]+)\s*\{([^}]*)\}/msi', $css, $matches);
        foreach ($matches[1] as $key => $classId) {
            $cssArray[$classId] = Array();
            $rawStyle = $matches[2][$key];
            $lines = explode(';', $rawStyle);
            foreach ($lines as $line) {
                $cssData = explode(':', trim($line));
                if (count($cssData) == 2) {
                    $cssArray[$classId][$cssData[0]] = $cssData[1];
                }
            }
        }
        return $cssArray;
    }
}